<template>
  <div>

    <div>
      <van-nav-bar title="收货地址列表"
                   left-text="返回"
                   @click-left="onClickLeft"
                   left-arrow
      />
    </div>

    <div class="container">
      <div>

        <keep-alive>
          <van-address-list
            v-model="chosenAddressId"
            :list="list"
            :disabled-list="disabledList"
            disabled-text="以下地址超出配送范围"
            default-tag-text="默认"
            @add="onAdd"
            @edit="onEdit"
            @select="onSelect"
          />
        </keep-alive>


      </div>

    </div>

  </div>
</template>

<script>
  import {queryShareInfo,addShareLy,queryShareLy,addShareStoreInfo ,addLyhfinfo,queryAgencyinfo,queryProductSku ,saveAgencyAddress ,queryAgenyAddress } from 'api/api'
  import {Toast} from 'vant'
  import AreaList from '../../../static/area'
  import eventBus from '../../utils/eventbus'


  export default {
    name: 'addressList',
    data() {
      return {
        list:[],
        chosenAddressId:'',
        disabledList:[],
        productId:'',
        skuId:'',
        selectedNum:'',
        price:'',
        totalPrice :'',
      }
    },
    created() {
      let agencyId = this.$route.query.agencyId
      if(!agencyId){
        agencyId = this.$store.getters.agencyId
      }
      this.agencyId =  agencyId
      console.log('agencyid:' , agencyId )
      this.queryAgenyAddress()

      this.productId =  this.$route.query.productId
      this.skuId =  this.$route.query.skuId
      this.selectedNum = this.$route.query.selectedNum
      this.price = this.$route.query.price
      this.totalPrice = this.price * this.selectedNum * 100

    },
    methods: {

      onClickLeft(){
        let data = {}
        data.productId = this.productId
        data.price = this.price
        data.totalPrice = this.totalPrice
        data.selectedNum = this.selectedNum
        data.skuId = this.skuId

        this.$router.go(-1) // 返回上一层
      },

      queryAddressList(){

      },

      onSave(data){

        console.log('地址：',data )
      },


      onAdd(){
        this.$router.push({ path: '/addAddress', query:{ agencyId:this.agencyId } })
      },

      onEdit(data){
        this.$router.push({ path: '/addAddress', query:{ addressId:data.addressId ,agencyId:this.agencyId} })
      },

      onSelect(data){
        data.productId = this.productId
        data.price = this.price
        data.totalPrice = this.totalPrice
        data.selectedNum = this.selectedNum
        data.skuId = this.skuId
        eventBus.$emit('addressInfo',data);
        //调用router回退页面
        this.$router.back();
      },

      queryAgenyAddress(){

        let param = {}
        param.agencyId  = this.agencyId
        queryAgenyAddress(param).then(res =>{
          if(res.code == 200 ){
            let list   = res.rows

            if(list.length > 0 ){
              for(var i=0;i<list.length;i++){
                if(list[i].mrflag == '1'){
                  list[i].isDefault  = true
                  this.chosenAddressId = list[i].addressId
                }

                list[i].address = list[i].province + list[i].city + list[i].county + list[i].addressDetail
                list[i].id = list[i].addressId
              }
            }
            this.list  = list
          }
        })
      }
    },

  }
</script>

<style scoped>

  .container{
    background-color: white;
  }

  .liuyan{
    background-color: white;
    margin-top: 20px;
    padding-bottom: 60px;
  }
  .columnr{
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
  }

  .rownr{
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: flex-start;
    width: 100%;
  }

  .name{
    padding-top: 10px;
    padding-left: 10px;
    font-size: 20px;
    color: #466BAC;
  }

  .img{
    padding-top: 10px;
    padding-left: 10px;
  }

  .content{
    font-size: 16px;
    padding:10px;
    text-indent: 2em;
  }

  .viewimg{
    width: 90%;
    padding-top: 10px;
    padding-left: 5%;
    padding-right: 4%;
  }

  .title_bg{
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    padding:10px
  }
  .title_sx{
    height: 20px;
    width: 3px;
    background-color: #f50;
  }
  .title_name{
    color:#333333;
    font-size: 20px;
    margin-left: 5px;
    font-weight: bold;
  }

  .hfcontent{
    padding-top: 10px;
    padding-left: 60px;
    font-size: 16px;
  }

  .lyban{
    width: 99%;
    position: fixed;
    bottom: 0;
    height: 50px;
    background-color: #ffffff;
    border-top: 1px solid #B2B2B2 ;
  }

  .lytextcss{
    margin-top:8px;
    margin-left:10px;
    border-radius:15px;
    border: 1px solid #B2B2B2;
    width: 90%;
    height: 30px;
  }

  .textareacss{
    margin-left:10px;
    margin-right:10px;
    margin-top: 10px;
    margin-bottom: 20px;
    border: 1px solid #B2B2B2;
  }

  .hfcss{
    padding-left: 60px;
    padding-top: 10px;
    font-size: 16px;
  }

</style>
